package com.timzer.springhive.ctrl;

import com.timzer.springhive.config.HiveConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * 使用 DataSource 操作 Hive
 * http://localhost:8080/hive/table/list
 * http://localhost:8080/hive/table/describe
 * http://localhost:8080/hive/table/select
 */
@RestController
@RequestMapping("/hive")
public class HiveDataSourceController {

    private static final Logger logger = LoggerFactory.getLogger(HiveDataSourceController.class);

    @Autowired
    HiveConfig hiveConfig;

    /**
     * 列举当前Hive库中的所有数据表
     */
    @GetMapping("/table/list")
    public List<String> listAllTables() throws SQLException {
        List<String> list = new ArrayList<String>();
        Connection connection= hiveConfig.dataSource().getConnection();
        Statement statement = connection.createStatement();
        String sql = "show tables";
        logger.info("Running: " + sql);
        ResultSet res = statement.executeQuery(sql);
        while (res.next()) {
            list.add(res.getString(1));
        }
        res.close();
        statement.close();
        connection.close();
        return list;
    }

    /**
     * 查询Hive库中的某张数据表字段信息
     */
    @GetMapping("/table/describe")
    public List<String> describeTable(String tableName) throws SQLException {
        List<String> list = new ArrayList<String>();
        // Statement statement = jdbcDataSource.getConnection().createStatement();
        Connection connection= hiveConfig.dataSource().getConnection();
        Statement statement = connection.createStatement();
        String sql = "describe " + tableName;
        logger.info("Running: " + sql);
        ResultSet res = statement.executeQuery(sql);
        while (res.next()) {
            list.add(res.getString(1));
        }
        res.close();
        statement.close();
        connection.close();
        return list;
    }

    /**
     * 查询指定tableName表中的数据
     */
    @GetMapping("/table/select")
    public List<String> selectFromTable(String tableName) throws SQLException {
        Connection connection= hiveConfig.dataSource().getConnection();
        Statement statement = connection.createStatement();
        String sql = "select * from " + tableName;
        logger.info("Running: " + sql);
        ResultSet res = statement.executeQuery(sql);
        List<String> list = new ArrayList<String>();
        int count = res.getMetaData().getColumnCount();
        String str = null;
        while (res.next()) {
            str = "";
            for (int i = 1; i < count; i++) {
                str += res.getString(i) + " ";
            }
            str += res.getString(count);
            logger.info(str);
            list.add(str);
        }
        res.close();
        statement.close();
        connection.close();
        return list;
    }

}
